package cn.rongdao.jrkabao.ui.information;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.rongdao.jrkabao.bean.information.apply.ApplyInfoBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.information.GetMyOwnLoanIncreaseAmountModel;
import cn.rongdao.jrkabao.ui.information.adapter.ApplyNeedInfoAdapter;
import cn.rongdao.jrkabao.util.growingio.GIOForBuringPointUtil;

/**
 * Created by jiangyongxing on 2016/9/10.
 * 描述：米粒贷款铁界面（资料完善界面）
 */
public class MyOwnLoanIncreaseAmountActivity extends BaseApplyNeedInfoActivity {


    public static final int RESULT_CODE_MYOWNLOANINCREASEAMOUNTACTIVITY_RETURN = 101;
    private List<ApplyInfoBean.MustListEntity> mMustListEntities = new ArrayList<>();
    private ApplyNeedInfoAdapter mApplyNeedInfoAdapter;
    private int mCheckType;

    private boolean isFirstRequest = true;

    private Map<String, String> mGioMap = new ArrayMap<>();

    public static void start(Activity context, String cateId, String productId) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.CATE_ID, cateId);
        bundle.putString(InfoConstant.PRODUCT_ID, productId);
        FRStartActivity.startForResult(context, MyOwnLoanIncreaseAmountActivity.class, bundle, 1);
    }

    protected void initData() {
        try {
            mCateId = getIntent().getStringExtra(InfoConstant.CATE_ID);
            mProductId = getIntent().getStringExtra(InfoConstant.PRODUCT_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //表示请求的是必填项目的数据
        orderParmasMap.put("show_module", "must");


        mGioMap.put("product_name", GIOForBuringPointUtil.getProductName());
        mGioMap.put("product_type", mCateId);


    }

    @Override
    public void initView() {
        super.initView();
        initData();
        initTitleBar();
        initListener();

    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        mApplyNeedInfoAdapter = new ApplyNeedInfoAdapter(this, mMustListEntities);
        return mApplyNeedInfoAdapter;
    }

    private void initListener() {
        mTitleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CODE_MYOWNLOANINCREASEAMOUNTACTIVITY_RETURN);
                finish();
            }
        });

        mApplyNeedInfoAdapter.setOnItemActionListener(listener);

    }


    @Override
    protected void initTitleBar() {
        super.initTitleBar();
        mTitleBarUIComponent.initTitleBlack("提升额度");
    }


    @Override
    protected void requestData() {
        GetMyOwnLoanIncreaseAmountModel.getInstance().execute(orderParmasMap, new FRBaseObserver<ApplyInfoBean>(this, isFirstRequest ? "正在加载" : null) {
            @Override
            protected void onBaseNext(ApplyInfoBean data) {
                isFirstRequest = false;

                mMustListEntities.clear();
                mMustListEntities.addAll(data.getMust_list());
                mApplyNeedInfoAdapter.notifyDataSetChanged();
                mCheckType = data.getCheck_type();

                if (mCheckType == -1) {
                    startRun();
                } else {
                    stopRunning();
                }
            }

        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_CODE_MYOWNLOANINCREASEAMOUNTACTIVITY_RETURN);
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}